t
Method Super Operations

Sometimes you want to have it both ways. You want a subclass to exhibit the behavior of its parent, but you want to add something new. The*215* super annotation  *1025*of method operations gives you this ability to inherit method behavior and then add to it. 

Assume that dogs sleep like the rest of the barnyard’s animals. In addition, dogs enter a dream state in which they sniff out and chase imagined rabbits. A Super annotation in an overshadowing version of the sleep method in the Dog class lets Dog instances dream when they sleep.

u
Copy the sleep method from the Animal class and paste it into the Dog Methods window.

u
Open the Dog/sleep method and select all the operations except the input and output bars. Select Opers to Local from the Opers menu. Name the resulting local method …and then dreams. Before editing this local to implement the dream state, you must first instruct the dog to sleep.

u
Create an operation and label it //sleep. Attach its left terminal to the left root of the input bar and its right terminal to the right root of the input bar. If you asked a Dog instance to sleep with this method in its current state, an infinite loop would result from the sleep method calling itself over and over until you issue a Break and an Abort command.

___________________________________________________________
NOTE:  You may have to move aside the comments on the two terminals of the sleep operation in order to see the terminals. Alternatively, Command-click the terminals to toggle their comments from being shown to being hidden.
-----------------------------------------------------------

u
Click on the //sleep operation and then select the*1026**592* Super item in the Controls menu. Your Dog/sleep method should now look like this:

The Super annotation tells Prograph to look for the sleep method, not in the Dog class but in the Animal class, the parent of the Dog subclass. If Prograph does not find the method in the parent of the current class, it continues up the inheritance line until it finds a method by the indicated name.

The Super annotated //sleep method operation produces an entry such as "Rover is sleeping." which is added to the DiaryEntry record of events. To complete the Dog/sleep method:

u
Select the //sleep operation, and then Option-click the …and then dreams local operation to create a synchro link. This ensures that the Dog instance sleeps before it dreams.

u
Open the …and then dreams local operation. Open a Change Value dialog on the string constant that is fed into the "join" operation. Change the " is sleeping." value to " enters a dream state and twitches as it sniffs and chases an imaginary rabbit."

u
Switch the order of the joined strings in the second "join" operation so the dream sequence is appended to the entry in the diary that notes that the Dog instance is sleeping.

u
After editing the local, close its case window and turn Step/Show On for the Dog/sleep method.

u
To test the Dog/sleep method without waiting for a random sleeping Dog event in the Barnyard Simulation, create a Dog/testSleep method. Note the input to the Dog instance generator is a list of (attributeName value) lists—thus the double parentheses, ((name Rover)), as this list only has one list element. Be sure to include the synchro so the /sleep operation executes before the what happened attribute is retrieved from the DiaryEntry for display.

u
Execute the Dog/testSleep method. An execution window on Dog/sleep opens as soon as Dog/testSleep calls it. Press Return to step execution to the Super annotated //sleep operation. Press Shift-Return to “step into” the call to Animal/sleep. Open the Stack window to confirm the progression of method executions.

u
Continue pressing Return to follow the flow of execution. After both Animal/sleep and Dog/sleep are resolved, Dog/testSleep finishes execution with the display of a Value window on the what happened attribute of the active DiaryEntry. The entry reads "Rover is sleeping. Rover enters a dream state and twitches as it sniffs and chases an imaginary rabbit."

____________________________________________________
NOTE:  Super annotations are most often used to incorporate the behavior of a parent’s version of a method within the overshadowing version of the method in the parent’s subclasses. A typical example is the use of the Super annotation to incorporate the behavior of the Animal/sleep method into the behavior of Dog/sleep.

Super annotations, however, can be used in a wide range of situations. A Super annotation can be used any time an instance should behave in a manner consistent with one of its parent’s methods rather than behave as it would if its own overshadowed version of a method were executed. 

An example is an object-oriented expert system that understands the domain of rocks and minerals. A subclass of mineral can behave one way at room temperature but behave like an instance of its parent class when heated. A method to assay this subclass of mineral can use case structure to test for temperature. The first case, which executes when the mineral is at room temperature, can use a method call, /shatterPressure, within the mineral subclass’s own collection of methods. A second case of the assaying method can execute when high temperatures are detected. This second case uses a Super annotated call to //shatterPressure, the parent’s version of this method. The range of the possible uses of the super annotation is beyond the scope of this tutorial. The mechanism itself and how you annotate a method-calling operation in the editor is the same for all such applications.
-----------------------------------------------------------

u
Before running the Barnyard simulation, copy and paste the Dog/sleep method to the Cat class and change the chase to mouse from rabbit.

As you run the Barnyard Simulation, you see increasingly diverse and descriptive entries in the diary.

t
Initialization, Get, and Set Methods

In addition to the basic attribute and method varieties, there are three special cases of overshadowing:  Instance generation, Get attribute, and Set attribute overshadowing. Reflecting the special nature of these situations, visually distinctive icons*410* *411**420*are used in the Methods windows of classes to identify methods that implement these features.

The shapes of these special method icons*977**388* are complementary to the shapes of the primitive operation icons that these methods overshadow. These unique icons are created by selecting the Instance, Get, or Set items in the Opers menu while a method icon is selected in a Methods window. 

 Overshadowing Instance Creation

An overshadowing Instance generator is assigned the special name, <<>>, automatically by the editor. You give overshadowed*386* Get and *975*Set attribute methods the name of the attribute that they are to overshadow.

The best way to further explain what these special methods are and how they work is by example. Let’s assume the research project landed some additional funding from the International Cattle Association (ICA). The ICA requires the researchers to report on the size of the cattle herds in the Barnyard they are studying. The reporting requirement is simply that the researchers telephone ICA headquarters once a year to report the number of cows, by species, owned by Farmer Brown.

To avoid having to sort through all the animals inhabiting the Barnyard at reporting time, use the class attribute herdMembers and overshadow the instance generation of Cow and, by inheritance, its subclass instances.

u
Open the Cow Methods window and Command-click to create a new method icon*482*. Select Instance from the Opers menu to transform the unnamed method into the instance-generation overshadowing*817* (*481**688*Initialization) method, which the editor automatically names <<>> .

___________________________________________________________
NOTE:  The*6* <<>> method (it is convenient to read and think of this as the “initialization” method) can be thought of as a user-defined automatic complement to the Instance primitive operation. That is why the <<>> method has a single root and terminal on its input and output bar. Once this method is defined in a class, <<>> is called anywhere there is an Instance primitive operation that generates an instance of the class in which the <<>> method is defined. Since the Instance primitive has a terminal and root on its operation icon, the <<>> method must maintain both the arity (the same number of roots and terminals) and the functionality of the Instance primitive operation that it overrides. Like its primitive counterpart, you can pass a list of (attributeName value) lists into the <<>> method to set the values of attributes of the instance that is being created. Once inside the <<>> method, your overshadowing method can do any additional processing you want to tailor the act of instance creation. It is then up to you to provide the datalink to output the massaged instance through the terminal of the <<>> output bar. If the newly created instance is not passed out the <<>> method’s output bar, the <<>> method produces a NONE data value along any datalink attached to its output root. You rarely want this to occur.

Instance generation overshadowing by means of an initialization method is typically used to do type checking on values that are to be set for attributes. You might want to make sure that an integer has been entered to a prompt for the wingspan of a Bird instance. Or you might want to check for capitalization of the name attributes of Animal instances, and so on.
-----------------------------------------------------------

When an instance of Cow or, by method inheritance, an instance of one of its subclasses, is generated, the new Cow instance joins the herd by being added to the list maintained in the class’s herdMembers class attribute. The object that flows into the <<>> method of Cow or its subclass is the new instance being created. Its name, age, and favorite food attribute values are assigned based on user input to the prompts that appear when the farmer goes to the auction every fourth iteration of the Barnyard/action method.

u
Open the Cow attribute window and click to select the herdMembers class attribute. Open a Value window on this attribute and set its default data type to list. Dismiss the Value window and, with the herdMembers attribute icon still selected, choose Propagate Attribute from the Edit menu. This sets the herdMembers attribute default value of the Cow subclasses to an empty list like their parent. 

u
Define the Cow/<<>> method so it looks like the following:

This <<>> method simply passes the newly created instance through from the input root to the output terminal. But before it does that, <<>> gets the current value of the new instance’s herdMembers attribute and attaches the new instance to the end of the list before setting herdMembers to this updated list value. This means that a separate herd is maintained in the Cow class and its subclasses.

u
To bring the Barnyard cows together in a herd—that is, to copy any Cow and its subclass instances from the Barnyard/inhabitants attribute to the herdMembers attribute of each of the Cow classes—selectively load the cowRoundUp universal method from the Chapter 6 Helpers, in the Ch 6 ƒ folder (inside the Tutorial Examples ƒ folder). Execute cowRoundUp.

u
Open a Value window on the herdMembers attribute in the Cow, Guernsey, and Black Angus classes to confirm the successful roundup. If no Cow or its subclass instances currently exist in your current Barnyard instance, open the inhabitants attribute in the Barnyard instance in Barnyard/activeYards and add a couple of Guernsey and Black Angus instances before going further. Execute cowRoundUp again and confirm that you now have some cows in your herdMember attributes. (You can delete cowRoundUp when you are done with this preliminary step, since Cow/<<>> updates the herd incrementally as new instances are created.)

With the Initialization method in the Cow class keeping an up-to-date list of members of the herds of the various species of cow, you now only need to add a reporting method to satisfy the ICA-funded program:

u
Open the Universal Methods window and then selectively load the herd report method from the Chapter 6 Helpers file.

The herd report method accesses the herdMembers attribute of the Cow class and its subclasses to determine the number of cows in each species herd. If you poke around a bit in this method you discover that case structure and various control annotations are used to prepare a grammatically appropriate report to ICA headquarters. For example, herd report produces such syntactic constructions as the phrases "There are no cows…", "There is 1…", and "There are 3…".

The researchers decide to schedule the annual ICA telephone report as an additional activity during the annual birthday celebration and trip to the animal auction.

u
To incorporate the ICA report into the annual birthday and auction attendance event, create a herd report operation in the birthday & buy? local method deeply nested in the Barnyard/action method.

u
Run the Barnyard Simulation for several time periods. Buy a Guernsey and Black Angus cow at your next two auctions.

Your annual birthday party and trip to the auction is accompanied by an ICA report display.

Certainly a simple integer-count attribute equal to the number of instances of each of the Cow classes would be more efficient for storage and reporting. The researchers, however, implemented the herdMembers instance list design with future reporting requirements in mind. As an informal challenge, consider extending the herd report method to compute an average age summary in addition to the total herd counts currently reported.